﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Globalization;

namespace SP.FeatureAdministrationTool.FeatureManagerModule
{
    public abstract class FeatureManager
    {
        public abstract void ActivateFeature(Guid featureId);

        public abstract void DeactivateFeature(Guid featureId);

        public abstract List<Feature> GetAllFeatures();

        protected List<Feature> GetFeatures(SPFeatureScope scope)
        {
            List<Feature> features = new List<Feature>();
            foreach (SPFeatureDefinition featureDefinition in SPFarm.Local.FeatureDefinitions)
            {
                try
                {
                    if (featureDefinition.Scope == scope)
                    {
                        Feature feature = new Feature()
                        {
                            Id = featureDefinition.Id,
                            DisplayName = featureDefinition.DisplayName,
                            Hidden = featureDefinition.Hidden,
                            Title = featureDefinition.GetTitle(CultureInfo.CurrentUICulture)
                        };

                        features.Add(feature);
                    }
                }
                catch
                {
                    continue;
                }
            }

            return features;
        }

        public static Dictionary<Feature, Exception> GetInvalidFeatures()
        {
            Dictionary<Feature, Exception> invalidFeatures = new Dictionary<Feature, Exception>();
            foreach (SPFeatureDefinition featureDefinition in SPFarm.Local.FeatureDefinitions)
            {
                string featureTitle = string.Empty;
                string displayName = string.Empty;
                Guid featureId = Guid.Empty;
                bool hidden = false;
                try
                {
                    featureId = featureDefinition.Id;
                    displayName = featureDefinition.DisplayName;
                    featureTitle = featureDefinition.GetTitle(CultureInfo.CurrentUICulture);
                    hidden = featureDefinition.Hidden;
                    SPFeatureScope scope = featureDefinition.Scope;
                }
                catch (Exception ex)
                {
                    if (string.IsNullOrEmpty(displayName))
                    {
                        displayName = ex.Message;
                    }
                    else if (string.IsNullOrEmpty(featureTitle))
                    {
                        featureTitle = ex.Message;
                    }

                    Feature feature = new Feature()
                    {
                        Id = featureId,
                        Title = featureTitle,
                        DisplayName = displayName,
                        Status = FeatureActivationStatus.Invalid,
                        Hidden = hidden
                    };

                    invalidFeatures.Add(feature, ex);
                }
            }

            return invalidFeatures;
        }

        // TODO: remove feature by CompatibilityLevel for SharePoint 2013
        public static void UninstallFeature(Guid featureId)
        {
            SPFarm.Local.FeatureDefinitions.Remove(featureId, true);
        }
    }
}
